﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Aesha.WoW.Broker.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Aesha.WoW.Broker.Infrastructure
{
    internal class BattleNetRepository
    {
        private readonly WebClient _webClient = new WebClient();

        public AuctionDataUpdateFile GetLatestAuctionDataUpdate()
        {
            var region = Properties.Settings.Default.Region;
            var realm = Properties.Settings.Default.Realm;
            var resource = GetJsonResource(new Uri(String.Format("http://{0}.battle.net/api/wow/auction/data/{1}", region, realm)));

            var jobj = JObject.Parse(resource);
            var jtoken = jobj["files"].FirstOrDefault();

            if (jtoken == null) return null;
            
            var auctionDataUpdateFile = JsonConvert.DeserializeObject<AuctionDataUpdateFile>(jtoken.ToString());
            return auctionDataUpdateFile;
        }

        public Item GetItem(int itemId)
        {
            try
            {
                var region = Properties.Settings.Default.Region;
                var resource = GetJsonResource(new Uri(String.Format("http://{0}.battle.net/api/wow/item/{1}", region, itemId)));

                var item = JsonConvert.DeserializeObject<Item>(resource);
                return item;
            }
            catch (Exception)
            {
                return null;
            }
           
        }

        public List<ItemClass> GetItemClasses()
        {
            var region = Properties.Settings.Default.Region;
            var resource = GetJsonResource(new Uri(String.Format("http://{0}.battle.net/api/wow/data/item/classes", region)));

            var jobj = JObject.Parse(resource);
            var jtoken = jobj["classes"];

            if (jtoken == null) return null;

            var itemClasses = JsonConvert.DeserializeObject<List<ItemClass>>(jtoken.ToString());

            foreach (var ic in itemClasses)
            {
                foreach (var isc in ic.SubClasses)
                {
                    isc.ItemClass = ic;
                    isc.ItemClassId = ic.ItemClassId;
                }
            }

            return itemClasses;
        }

        public List<AuctionListing> GetListings(Uri uri)
        {
            var resource = GetJsonResource(uri);

            var auctionDataFile = JsonConvert.DeserializeObject<AuctionDataFile>(resource);
            switch (Properties.Settings.Default.Faction)
            {
                case "Alliance":
                    {
                        return auctionDataFile.Alliance.Auctions;
                    }
                case "Horde":
                    {
                        return auctionDataFile.Horde.Auctions;
                    }
                case "Neutral":
                    {
                        return auctionDataFile.Neutral.Auctions;
                    }
                default:
                    {
                        return auctionDataFile.Alliance.Auctions;
                    }
            }
        }

        private string GetJsonResource(Uri uri)
        {
            try
            {

                _webClient.Proxy = null;
                var resource = _webClient.DownloadString(uri);

                FileStream fs = File.Open(@"c:\temp\" + DateTime.Now.Ticks + ".json", FileMode.Create, FileAccess.Write);
                var sw = new StreamWriter(fs);

                sw.Write(resource);
                sw.Flush();
                sw.Close();

                return resource;
            }
            catch (Exception)
            {
                throw;
            }
           
        }

    }
}
